#!/usr/bin/env python3.8
# -*- encoding: utf-8 -*-
'''
@File    :   views.py
@Time    :   2021/10/11 21:14:44
@Author  :   windstarry 
@Version :   1.0
'''
# here put the import lib
from flask import render_template, abort, request, url_for, send_from_directory, redirect,flash
from flask_login import current_user,login_required
from app.user import user_bp
from app.models import User
from app.ext import avatars, db

@user_bp.route('/<username>')
def user_profile(username):
    user = User.query.filter_by(username=username).first()
    if user is None:
        abort(404)
    return render_template('user/user-profile.html', user=user)


@user_bp.route('/avatars/<path:filename>')
def get_avatar(filename):
    return send_from_directory('app/static/avatars', filename)


@user_bp.route('/change-avatar', methods=['GET', 'POST'])
@login_required
def upload():
    if request.method == 'POST':
        f = request.files.get('file')
        raw_filename = avatars.save_avatar(f)
        u = current_user._get_current_object()
        u.raw_avatar = raw_filename
        db.session.add(u)
        db.session.commit()
        return redirect(url_for('user.crop'))
    return render_template('user/change-avatar-upload.html',title='上传头像')

@user_bp.route('/change-avatar/crop', methods=['GET', 'POST'])
@login_required
def crop():
    if request.method == 'POST':
        x = request.form.get('x')
        y = request.form.get('y')
        w = request.form.get('w')
        h = request.form.get('h')
        filenames = avatars.crop_avatar(current_user.raw_avatar, x, y, w, h)
        u = current_user._get_current_object()
        u.avatar_s = filenames[0]
        u.avatar_m = filenames[1]
        u.avatar_l = filenames[2]
        db.session.add(u)
        db.session.commit()
        flash('更改头像成功', 'success')
        return redirect(url_for('user.user_profile', username=u.username))
    return render_template('user/change-avatar-crop.html',title='更换头像')


@user_bp.route('/change-avatar/default')
@login_required
def default_avatar():
    u = current_user._get_current_object()
    u.avatar_l = '%s_l.png' % u.username
    u.avatar_m = '%s_m.png' % u.username
    u.avatar_s = '%s_s.png' % u.username
    db.session.add(u)
    db.session.commit()
    flash('头像已恢复为默认头像', 'success')
    return redirect(url_for('user.user_profile', username=u.username))